package pure.ui.core 
{
    import pure.ui.skins.*;
    import flash.display.Bitmap;
    import flash.events.*;
    import flash.geom.Point;
    import flash.text.*;
    import flash.filters.DropShadowFilter;
	
	/**
	 * 开关按钮组件基类
	 * @author des
	 * 
	 */
	
public class ToggleButton extends LabelComponent
{
	public function ToggleButton(x:Number, y:Number, size:Point, skin:ISkin,hasLabel:Boolean)
	{
		super(x, y, size,skin, hasLabel );
	}
	

	/*model*/
	
	protected var _selected:Boolean ;
	
	protected var _isOver:Boolean;

	
	
	
	
	
	/* ----------------------------------------------------    get / set    ----------------------------------------------------*/
	
	
	
	
	
	// 开关按钮皮肤
	override public function set skin(value:ISkin):void
	{
		if (!value is SelectableSkin)
		{
			throw new Error('ToggleButton::skin - 皮肤类型错误！');
		}
		super.skin = value;
	}	

	// 是否被选择
	public function set selected(b:Boolean):void
	{
		if (this._selected != b)
		{
			this._selected = b;
			update();
		}
	}
	
	public function get selected():Boolean
	{
		return _selected;
	}

	
	
	
	
	
	/* ----------------------------------------------------    private    ----------------------------------------------------*/
	
	
	
	
	
	
	override protected function _initSelf():void
	{
		super._initSelf();
		this.mouseChildren = false;
		this._isOver=false;
		this._selected = false;
		this._padding = 12;
		this.addEventListener(MouseEvent.ROLL_OVER, ____onMouseOver);
		this.addEventListener(MouseEvent.CLICK, ____onClick);
	}
	
	/**
	 * 子元素
	 **/
	override protected function _addChildren():void
	{
		super._addChildren();
		this._bitmap = new Bitmap();
		addChild(this._bitmap);
	}
	
	/**
	 * 滑过
	 **/
	protected function ____onMouseOver(e:MouseEvent):void
	{
		this._isOver = true;
		this.addEventListener(MouseEvent.ROLL_OUT, ____onMouseLeave);
		update();
	}
	
	/**
	 * 移出
	 **/
	protected function ____onMouseLeave(e:MouseEvent):void
	{
		this.removeEventListener(MouseEvent.ROLL_OUT, ____onMouseLeave);
		this._isOver = false;
		update();
	}
	
	/**
	 * 点击
	 **/
	protected function ____onClick(e:MouseEvent):void
	{
		_setSelectState();
	}
	

	/**
	 * 更换外观
	 **/
	protected function _selectView(over:uint,out:uint,selectedOver:uint,selectedOut:uint):void
	{
		// 滑过
		if (this._isOver)
		{
			if (this._skin)
			{
				// 选中滑过
				if (this._selected)
				{
					if (_getBitmap("selectedOver"))
					{
						_skip("selectedOver");
						_clear();
					}
					else 
					{
						if (this._bitmap.bitmapData) 
						{
							this._bitmap.bitmapData = null;
						}
						_definition = null;
						_drawSelectedFlag(selectedOver, true);
					}
				}
				
				// 未选滑过
				else 
				{
					if (_getBitmap("over")) 
					{
						_skip("over");
						_clear();
					}
					else 
					{
						if (this._bitmap.bitmapData) 
						{
							this._bitmap.bitmapData = null;
						}
						_definition = null;
						_drawSelectedFlag(over,false);
					}
				}
			}
			else
			{
				_drawSelectedFlag(this._selected?selectedOver:over,this._selected);
			}
			return;
		}
		
		// 移出
		if (this._skin) 
		{
			// 选中移出
			if (this._selected) 
			{
				if (_getBitmap("selectedOut")) 
				{
					_skip("selectedOut");
					_clear();
				}
				else 
				{
					if (this._bitmap.bitmapData) 
					{
						this._bitmap.bitmapData = null;
					}
					_definition = null;
					_drawSelectedFlag(selectedOut, true);
				}
			}
			
			// 未选移出
			else 
			{
				if (_getBitmap("out")) 
				{
					_skip("out");
					_clear();
				}
				else 
				{
					if (this._bitmap.bitmapData) 
					{
						this._bitmap.bitmapData = null;
					}
					_definition = null;
					_drawSelectedFlag(out,false);
				}
			}
		}
		else
		{
			_drawSelectedFlag(this._selected?selectedOut:out,this._selected);
		}
	}

	/**
	 * 缺省皮肤
	 * @param fillColor			填充颜色
	 * @param isSelected		是否选中状态
	 **/
	protected function _drawSelectedFlag(fillColor:uint,isSelected:Boolean):void
	{
		// 在子类中实现！
	}

	/**
	 * 改变选择状态
	 **/
	protected function _setSelectState():void
	{
		// 在子类中实现！
	}
		
	/**
	 * 调整位置
	 **/
	override protected function _rejust():void
	{
		this._textField.x = this._width * 0.5 + this._padding;
		this._textField.y = - this._textField.height * 0.5;
	}
}

}